package com.hf.wujian.netty;

import com.hf.wujian.model.imp.BSHGameHall;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.util.Map;

/**
 * 解码处理类
 *
 * @author hf
 * @date 2018/11/23
 * @version 1.0
 */
public class BSHServerInitializer extends ChannelInitializer<SocketChannel> {

    private BSHGameHall gameHall;
    private Map<Integer,Class> dispatchs;

    public BSHServerInitializer(BSHGameHall gameHall,Map<Integer,Class> dispatchs){
        this.gameHall = gameHall;
        this.dispatchs = dispatchs;
    }

    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();

        // 以("\n")为结尾分割的 解码器
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));

        // 字符串解码 和 编码
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());

        // 自己的逻辑Handler
        pipeline.addLast("handler", new BSHServerHandler(gameHall,dispatchs));
    }
}
